//
//  HZL_ZoneViewController.m
//  B_Project
//
//  Created by dllo on 15/11/28.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_ZoneViewController.h"
#import "HZL_NetworkingHandle.h"
#import "HZL_FindModel.h"
#import "HZL_ZoneDetailTableViewCell.h"
#import "HZL_StoreDetailViewController.h"

@interface HZL_ZoneViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, copy) NSString *cityId;

// 刷新
@property (nonatomic, assign) NSInteger MJI;
@property (nonatomic, assign) BOOL isRefresh;
@property (nonatomic, retain) MJRefreshAutoStateFooter *footer;
@end

@implementation HZL_ZoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.dataArr = [NSMutableArray array];
    [self creatTableView];
//    [self getData];
    [self loadMBProgress];
    
}
- (void)changeCity:(NSNotification *)note
{
    self.cityId = note.object;
    [self.tableView.mj_header beginRefreshing];
    [self getData];
}
#pragma mark - 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableView];
    // 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isRefresh = YES;
        self.MJI = 1;
        [self getData];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    // 上拉加载
    self.footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        self.isRefresh = NO;
        self.MJI ++;
        [self getData];
    }];
    self.tableView.mj_footer = self.footer;

    
}
- (void)getData
{
    NSString *city = [[NSUserDefaults standardUserDefaults] objectForKey:@"city"];
    if (city == nil) {
        city = @"xiamen";
    }

    if (!self.businessDistrictId) {
        NSString *body = [NSString stringWithFormat:@"cityId=%@&currentPage=%ld&device=iPhone7%%2C2&latitude=38.88269973986771&longitude=121.5393291161015&netWork=wifi&os=ios%%209.100000&pageSize=10", city,self.MJI];
        [HZL_NetworkingHandle requestPOSTWithURl:@"http://www.molyo.com/mShop/getShopList" body:body completion:^(id result) {

            
            self.hud.hidden = YES;
            if (!result) {
                [self.view bringSubviewToFront:self.alertBtn];
                return;
            }
            
            // 下拉刷新
            if (self.isRefresh) {
                [self.dataArr removeAllObjects];
            }
            // 上拉加载
            
            if ([(NSArray *)result[@"body"][@"list"] count] != 0) {
                [self.dataArr addObjectsFromArray:[HZL_FindModel transformWithArray:result[@"body"][@"list"]]];
                [self.footer setTitle:@"点击或上拉加载" forState:MJRefreshStateIdle];
            }else {
                // 是否加载完
                [self.footer setTitle:@"已经到底了" forState:MJRefreshStateIdle];
            }
            
            self.alertBtn.hidden = YES;
            [self.hud removeFromSuperview];
            
            [HZL_NetworkingHandle networkReachabilityStatus:^(id r) {
                if (![r isEqualToNumber:@0]) {
                    [self.tableView.mj_footer endRefreshing];
                }
            }];
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
            
        }];
    }
    NSString *body = [NSString stringWithFormat:@"businessDistrictId=%@&cityId=%@&currentPage=%ld&device=iPhone7%%2C2&latitude=22.61667&longitude=114.06667&netWork=wifi&os=ios%%209.100000&pageSize=10", self.businessDistrictId, city,self.MJI];
    [HZL_NetworkingHandle requestPOSTWithURl:@"http://www.molyo.com/mShop/getShopList" body:body completion:^(id result) {
        self.hud.hidden = YES;
        if (!result) {
            [self.view bringSubviewToFront:self.alertBtn];
            return;
        }
        // 下拉刷新
        if (self.isRefresh) {
            [self.dataArr removeAllObjects];
        }
        // 上拉加载
        if ([(NSArray *)result[@"body"][@"list"] count] != 0) {
            [self.dataArr addObjectsFromArray:[HZL_FindModel transformWithArray:result[@"body"][@"list"]]];
            [self.footer setTitle:@"点击或上拉加载" forState:MJRefreshStateIdle];
        }else {
            // 是否加载完
            [self.footer setTitle:@"已经到底了" forState:MJRefreshStateIdle];
        }
        
        self.alertBtn.hidden = YES;
        [self.hud removeFromSuperview];
        
        [HZL_NetworkingHandle networkReachabilityStatus:^(id r) {
            if (![r isEqualToNumber:@0]) {
                [self.tableView.mj_footer endRefreshing];
            }
        }];
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    }];
}
#pragma mark - tableView方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HZL_ZoneDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[HZL_ZoneDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.model = self.dataArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (VIEW_HEIGHT - 64 + 10) / 3 + 20 ;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HZL_StoreDetailViewController *sdVC = [[HZL_StoreDetailViewController alloc] init];
    sdVC.iD = [self.dataArr[indexPath.row] iD];
    sdVC.findModel = self.dataArr[indexPath.row];
    [self.navigationController pushViewController:sdVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
